package cn.hzw.supermarket.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.adapter.GuidePagerAdapter;
import cn.hzw.supermarket.databinding.ActivityGuideBinding;
import cn.hzw.supermarket.util.DensityUtil;

/**
 * 引导页
 */
public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityGuideBinding binding;
    private static final String  TAG = "GuideActivity";
    private GuidePagerAdapter guidePagerAdapter;
    private List<View> views;
    TextView circleOrange; //会移动的小圆点

    int leftMargin;//左边距 容器LinearLayout
    int separationDistance=40;//间距
    int dip = 15;//大小
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
        initEvent();

    }

    /**
     * 初始化视图和布局
     */
    private void initView() {
        if (guidePagerAdapter == null){
            guidePagerAdapter = new GuidePagerAdapter();
        }
        int[] resIds = guidePagerAdapter.getTitles();
        //设置背景图片和不会移动的背景小圆点
        for (int i = 0;i<resIds.length;i++){
            //设置点的大小 单位为px 设置屏幕适配
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    DensityUtil.dip2px(this,dip),DensityUtil.dip2px(this,dip));
            View circleWhite = new TextView(this);
            circleWhite.setBackgroundResource(R.drawable.circle_white);
            //设置间隙，过滤第一个点
            if (i>0){
                params.leftMargin = DensityUtil.dip2px(this,separationDistance);
            }
            circleWhite.setLayoutParams(params);
            binding.guideBackgroundIndicator.addView(circleWhite,i);
            ImageView imageView = new ImageView(this);
            Glide.with(this).load(resIds[i]).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            guidePagerAdapter.getViews().add(imageView);

            //设置会移动的前景小圆点
            circleOrange = new TextView(this);
            circleOrange.setBackgroundResource(R.drawable.circle_orange);
            binding.guideForegroundIndicator.addView(circleOrange);

            views = guidePagerAdapter.getViews();
            binding.viewPager.setAdapter(guidePagerAdapter);

            leftMargin = binding.guideBackgroundIndicator.getLeft();
        }
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        binding.goMainBtn.setOnClickListener(this);
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //第几个界面,滑动偏移百分比，滑动偏移像素
                Log.d(TAG, "onPageScrolled() called with:position = ["+position+"],positionOffset=["+positionOffset+"]," +
                        "positionOffsetPixels=["+positionOffsetPixels+"]");
                //计算circle_orange_id的左边距
                float left = (separationDistance+dip)*(position+positionOffset);
                Log.d(TAG, "leftMargin--left: "+leftMargin+"--"+left);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        DensityUtil.dip2px(GuideActivity.this,dip),DensityUtil.dip2px(GuideActivity.this,dip)
                );
                params.leftMargin = DensityUtil.dip2px(GuideActivity.this,left);
                circleOrange.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected() called with:position= ["+position+"]");
                AlphaAnimation fadeIn = new AlphaAnimation(0f,1f);
                fadeIn.setDuration(1500);
                AlphaAnimation fadeOut = new AlphaAnimation(1f,0f);
                fadeOut.setDuration(1500);
                //最后一页
                if (position==guidePagerAdapter.getViews().size()-1){
                    binding.goMainBtn.setVisibility(View.VISIBLE);
                    binding.goMainBtn.startAnimation(fadeIn);
                    binding.goMainBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(GuideActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }else {
                    binding.goMainBtn.setVisibility(View.GONE);
                    binding.guideIndicator.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.go_main_btn:
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                overridePendingTransition(R.anim.activity_left_in,R.anim.activity_rignt_out);
                finish();
                break;
            default:
                break;
        }
    }
}